﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RailwayCompany.ViewModel;
using RailwayDAL;
using System.ComponentModel;

namespace RailwayCompany
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ViewModelMain vmMain;
        Polyline poly;
        public MainWindow()
        {
            InitializeComponent();
            vmMain = new ViewModelMain();
            this.DataContext = vmMain;
            AddButtons();
        }

        private void AddButtons()
        {
            var stations = vmMain.Stations;
            foreach (var station in stations)
            {
                Button button = new Button();
                button.Content = station.StationName;
                button.Margin = new Thickness(station.OffsetX, station.OffsetY, 0, 0);
                button.Click += StationButton_Click;
                TrainMap.Children.Add(button);
            }
        }

        private void StationButton_Click(object sender, RoutedEventArgs e)
        {
            string stationName = (sender as Button).Content.ToString();
            Station station = RailwayDBManager.GetStationByName(stationName);
            List<Route> trains = RailwayDBManager.GetTrainsPassingThroughStation(station.StationID);
            TrainsList.ItemsSource = trains;
        }

        private void TrainSelect_Button_Click(object sender, RoutedEventArgs e)
        {
            int routeID = int.Parse((sender as Button).DataContext.ToString());
            Route route = RailwayDBManager.GetRouteByID(routeID);
            List<Station> stations;
            List<TimeTable> timetables = RailwayDBManager.CalculateTimetable(route, out stations);
            TimeTableList.ItemsSource = timetables;
            TrainMap.Children.Remove(poly);
            LinkStations(stations);
        }

        private void LinkStations(List<Station> stations)
        {
            poly = new Polyline();
            poly.Name = "Poly";
            foreach (Station station in stations)
            {
                poly.Points.Add(new Point(station.OffsetX, station.OffsetY));
            }
            poly.Stroke = new SolidColorBrush(Colors.Green);
            poly.StrokeThickness = 10;
            TrainMap.Children.Add(poly);
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            Station firstStation = FirstStationListBox.SelectedValue as Station;
            Station secondStation = SecondStationListBox.SelectedValue as Station;
            SortedDictionary<TimeSpan, Route> directRoutes = RailwayDBManager.GetAllDirectTrains(secondStation.StationID, firstStation.StationID);
            List<Route> directs = new List<Route>();
            foreach (var item in directRoutes)
            {
                Route route = new Route()
                {
                    DepartureTime = item.Key,
                    RouteStationNames = item.Value.RouteStationNames
                };
                directs.Add(route);
            }
            DirectRoutesList.ItemsSource = directs;
        }
    }
}
